package com.dhj.streamTest;

import com.dhj.streamTest.entity.Author;

import java.util.Comparator;
import java.util.List;
import java.util.function.Consumer;

import static com.dhj.streamTest.entity.AuthorList.getAuthors;

public class Demo6 {

    public static void main(String[] args) {
        List<Author> authors = getAuthors();


        /**
         * 如果调用空参的sorted() 方法，需要流中元素实现Comparable方法。
         */
        authors.stream().distinct().sorted().forEach(new Consumer<Author>() {
            @Override
            public void accept(Author author) {
                System.out.println(author.getName() + "--" + author.getAge());
            }
        });


        System.out.println("--------------------------------");

        authors.stream().distinct().sorted(new Comparator<Author>() {
            /**
             * o1-o2：升序
             * o2-o1：降序
             */
            @Override
            public int compare(Author o1, Author o2) {
                return o2.getAge() - o1.getAge();
            }
        }).forEach(new Consumer<Author>() {
            @Override
            public void accept(Author author) {
                System.out.println(author.getName() + "--" + author.getAge());
            }
        });

    }
}
